package com.yearner.reviewplan.manage.system.service.impl;

import com.yearner.reviewplan.manage.system.bean.RouterFactory;
import com.yearner.reviewplan.manage.system.bean.RouterVo;
import com.yearner.reviewplan.manage.system.dao.UserRouterDao;
import com.yearner.reviewplan.manage.system.entity.UserPerm;
import com.yearner.reviewplan.manage.system.entity.UserRouter;
import com.yearner.reviewplan.manage.system.service.UserPermService;
import com.yearner.reviewplan.manage.system.service.UserRouterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 路由表 服务实现类
 *
 * @author Pengyg
 * @since 2020-01-10
 */
@Service("UserRouterService")
public class UserRouterServiceImpl extends ServiceImpl<UserRouterDao, UserRouter> implements UserRouterService {
    @Autowired
    UserPermService userPermService;

    @Override
    public void addByPerm(Integer[] ids) {
        List<UserPerm> permList = userPermService.listByIds(Arrays.asList(ids));
        if (permList.isEmpty()) {
            return;
        }
        addByPerm(permList);
    }

    @Override
    public void addByPerm(List<UserPerm> permList) {
        List<UserRouter> insertList = new ArrayList<>();
        for (UserPerm perm :  permList) {
            UserRouter router = RouterFactory.byPerm(perm);
            // todo 根据实际用户
            if (StringUtils.isEmpty(router.getCreator())) {
                router.setCreator("admin");
            }
            insertList.add(router);
        }
        saveBatch(insertList);
    }

    @Override
    public List<RouterVo> getRouterVos() {
        // todo 根据权限
        List<UserRouter> list = baseMapper.selectNotLockRoot();
        List<RouterVo> routerVos = new ArrayList<>();
        for (UserRouter r : list) {
            routerVos.add(traversePerm(r,true));
        }
        return routerVos;
    }

    /**
     * 获取所有的路由
     * @return
     */
    @Override
    public List<RouterVo> getAllRouter() {
        List<UserRouter> list = baseMapper.selectRoot();
        List<RouterVo> routerVos = new ArrayList<>();
        for (UserRouter r : list) {
            routerVos.add(traversePerm(r,false));
        }
        return routerVos;
    }

    @Override
    public void deleteById(List<Integer> ids) {
        List<UserRouter> willDelete = listByIds(ids);
        List<UserRouter> updateChild = new ArrayList<>();
        // 如果它有子节点，则将子节点改为它的父节点
        for (UserRouter dd : willDelete) {
            List<UserRouter> children = baseMapper.getChildren(dd.getRouterId());
            for (UserRouter r :
                    children) {
                if (ids.contains(r.getRouterId())) {
                    continue;
                }
                r.setParentId(dd.getParentId());
                updateChild.add(r);
            }
        }
        removeByIds(ids);
        if (!updateChild.isEmpty()) {
            updateBatchById(updateChild);
        }
    }

    @Override
    public boolean save(UserRouter entity) {
        entity.setILock(RouterFactory.lockSuggest(entity));
        return super.save(entity);
    }


    private RouterVo traversePerm(UserRouter router,boolean filterLock) {
        // 新建一个菜单
        RouterVo aside = new RouterVo(router);

        List<UserRouter> childrenRouter = baseMapper.getChildren(router.getRouterId());
        List<RouterVo> childrenVo = new ArrayList<>();
        // 拥有子菜单
        for (UserRouter r : childrenRouter) {
            if (filterLock && r.getILock()) {
                continue;
            }
            aside.setChildren(childrenVo);

            RouterVo child = traversePerm(r,filterLock);
            childrenVo.add(child);
        }

        return aside;
    }
}
